﻿//获取字节数
function getByteLength(str) {
    var byteLen = 0, len = str.length;
    if (str) {
        for (var i = 0; i < len; i++) {
            if (str.charCodeAt(i) > 255)
                byteLen += 2;
            else
                byteLen++;
        }
        return byteLen;
    }
    else
        return 0;
}


var arrays = new Array();
var arrays2 = new Array();
var arrays3 = new Array();
var arrays4 = new Array();
function getTableContent() {
    var mytable = document.getElementById("mytable");
    var data = [];
    for (var i = 0, rows = mytable.rows.length; i < rows; i++) {
        for (var j = 0, cells = mytable.rows[i].cells.length; j < cells; j++) {
            if (!data[i]) {
                data[i] = new Array();
            }
            data[i][j] = mytable.rows[i].cells[j].innerHTML;
        }
    }
    for (var i = 0; i < data.length; i++) {
        arrays.push(data[i][0])
        arrays2.push(data[i][1])
        arrays3.push(data[i][2])
    }
    data = [];
    var myprint = document.getElementById("myprint")
    for (var i = 0, rows = myprint.rows.length; i < rows; i++) {
        for (var j = 0, cells = myprint.rows[i].cells.length; j < cells; j++) {
            if (!data[i]) {
                data[i] = new Array();
            }
            data[i][j] = myprint.rows[i].cells[j].innerHTML;
        }
    }
    for (var i = 0; i < data.length; i++) {
        arrays4.push(data[i][0] + ',' + data[i][1])
    }
};

var LODOP, TaskID1, TaskID2, c = 0, t, P_ID = "", loop = 0, waiting = false;
//title 标题   60mm
//sameNum 台位  phone 电话  payType 支付方式  payTime 支付时间
//arrays 第一列   arrays2 第二列   arrays3 第三列
//subTotal 小计  discount 优惠   handle  应付
//companyName  公司名    address  地址   serialNum  流水号   printTime  打印时间
function Print(title, phone,payType, payTime, arrays, arrays2, arrays3, subTotal, discount, handle, companyName, guest, guestphone, address, serialNum, printTime) {
    var lineHeight = 2;
    LODOP.PRINT_INIT(title + "/" + serialNum);//打印队列名称
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "50mm", "8.29mm", title);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 18);
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    lineHeight += 8.29;
    //LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "50mm", "5.29mm", "台位：" + sameNum);
    //lineHeight += 5.29;
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "50mm", "5.29mm", "流水号：" + serialNum);
    lineHeight += 5.29;
    if (getByteLength("流水号：" + serialNum) * 2 > 36) lineHeight += 5.29;
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "50mm", "5.29mm", "电话：" + phone);
    lineHeight += 5.29;
    //给予订单的状态   
    //未确认   确认中   已确认
    var payTypeTemp;
    if (parseInt(payType) < 60)
        payTypeTemp = '未确认'
    else if (parseInt(payType) == 60)
        payTypeTemp = '确认中';
    else
        payTypeTemp = '已确认'

    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "50mm", "5.29mm", payTypeTemp + "    " + payTime);
    lineHeight += 5.29;
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "50mm", "3.29mm", "-----------------------------------");
    lineHeight += 3.29;
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "34mm", "5.29mm", "品名");
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "34mm", "6mm", "5.29mm", "个");
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "40mm", "10mm", "5.29mm", "价格");
    lineHeight += 5.29;

    //开始加入产品
    //第一列
    var j = 0;
    var line = 0;
    var appline = 0;
    var topvalue = lineHeight;//保存前一个的位置
    var topvalue2 = lineHeight;
    var topvalue3 = lineHeight;
    var recordtop = 10;
    var recordtop2 = 10;
    var recordtop3 = 10;
    for (var i = 0; i < arrays.length; i++) {
        j = getByteLength(arrays[i]) * 2 > 36 ? 2 : 1;
        line = j == 2 && i != 0 ? (10 - recordtop) : 0;
        appline = j == 1 && line == 0 ? (10 - recordtop) : 0;
        topvalue += line;
        topvalue += appline;
        LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "34mm", (5.29 * j) + "mm", arrays[i]);
        recordtop = j == 2 ? 0 : 5.29;
        if (i == arrays.length - 1)
            topvalue += 10 - recordtop;
    }
    //第二列
    for (var i = 0; i < arrays2.length; i++) {
        j = getByteLength(arrays[i]) * 2 > 36 ? 2 : 1;
        line = j == 2 && i != 0 ? 10 - recordtop2 : 0;
        appline = j == 1 && line == 0 ? (10 - recordtop2) : 0;
        topvalue2 += line;
        topvalue2 += appline;
        LODOP.ADD_PRINT_TEXT(topvalue2 + "mm", "34mm", "6mm", "5.29mm", arrays2[i]);
        recordtop2 = j == 2 ? 0 : 5.29;
    }
    var totalNum = 0;
    //第三列
    for (var i = 0; i < arrays3.length; i++) {
        j = getByteLength(arrays[i]) * 2 > 36 ? 2 : 1;
        line = j == 2 && i != 0 ? 10 - recordtop3 : 0;
        appline = j == 1 && line == 0 ? (10 - recordtop3) : 0;
        topvalue3 += line;
        topvalue3 += appline;
        LODOP.ADD_PRINT_TEXT(topvalue3 + "mm", "40mm", "10mm", "5.29mm", arrays3[i].substr(0,6));
        recordtop3 = j == 2 ? 0 : 5.29;
        totalNum += parseFloat(arrays3[i]);
    }
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "50mm", "3.29mm", "-----------------------------------");
    topvalue += 3.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "10mm", "40mm", "5.29mm", "小计：" + subTotal);
    topvalue += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "10mm", "40mm", "5.29mm", "优惠：" + discount);
    topvalue += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "10mm", "40mm", "5.29mm", "应付：" + handle);
    topvalue += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "50mm", "5.29mm", "客人：" + guest);
    topvalue += 6.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "50mm", "5.29mm", "联系电话：" + guestphone);
    topvalue += 6.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "50mm", "5.29mm", "地址：" + address);
    topvalue += 5.29;
    if (getByteLength("地址：" + address) * 2 > 36) topvalue += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "50mm", "5.29mm", "打印时间：" + printTime);
    topvalue += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "15mm", "35mm", "6.29mm", "谢谢惠顾!");
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
    topvalue += 6.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "50mm", "5.29mm", companyName);
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    LODOP.SET_PRINT_PAGESIZE(3, 500, 0, "");//设置打印的宽高
}


//title 标题    80mm
//sameNum 台位  phone 电话 payType 支付方式  payTime 支付时间
//arrays 第一列商品   arrays2 第二列数量   arrays3 第三列价格
//subTotal 小计  discount 优惠   handle  应付
//companyName  公司名    address  地址   serialNum  流水号   printTime  打印时间
function Print2(title, phone,payType, payTime, arrays, arrays2, arrays3, subTotal, discount, handle, companyName, guest, guestphone, address, serialNum, printTime) {
    var lineHeight = 2;
    LODOP.PRINT_INIT(title + "/" + serialNum);//打印队列名称
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "74mm", "8.29mm", title);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 18);
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    lineHeight += 8.29;
    //LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "74mm", "6.29mm", "台位：" + sameNum);
    //LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    //LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
    //lineHeight += 6.29;
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "74mm", "5.29mm", "流水号：" + serialNum);
    lineHeight += 5.29;
    if (getByteLength("流水号：" + serialNum) * 2 > 72) lineHeight += 5.29;
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "74mm", "5.29mm", "电话：" + phone);
    lineHeight += 5.29;

    //给予订单的状态   
    //未确认   确认中   已确认
    var payTypeTemp;
    if (parseInt(payType) < 60)
        payTypeTemp = '未确认'
    else if (parseInt(payType) == 60)
        payTypeTemp = '确认中';
    else
        payTypeTemp = '已确认'

    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "74mm", "5.29mm", payTypeTemp + "    " + payTime);
    lineHeight += 5.29;
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "74mm", "3.29mm", "-------------------------------------------------------------------------------");
    lineHeight += 3.29;
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "0mm", "52mm", "5.29mm", "品名");
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "52mm", "10mm", "5.29mm", "数量");
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    LODOP.ADD_PRINT_TEXT(lineHeight + "mm", "62mm", "12mm", "5.29mm", "价格");
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    lineHeight += 5.29;

    //开始加入产品
    //第一列
    var j = 0;
    var line = 0;
    var appline = 0;
    var topvalue = lineHeight;//保存前一个的位置
    var topvalue2 = lineHeight;
    var topvalue3 = lineHeight;
    var recordtop = 10;
    var recordtop2 = 10;
    var recordtop3 = 10;
    for (var i = 0; i < arrays.length; i++) {
        j = getByteLength(arrays[i]) * 2 > 72 ? 2 : 1;
        line = j == 2 && i != 0 ? (10 - recordtop) : 0;
        appline = j == 1 && line == 0 ? (10 - recordtop) : 0;
        topvalue += line;
        topvalue += appline;
        LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "52mm", (5.29 * j) + "mm", arrays[i]);
        recordtop = j == 2 ? 0 : 5.29;
        if (i == arrays.length - 1)
            topvalue += 10 - recordtop;
    }
    //第二列
    for (var i = 0; i < arrays2.length; i++) {
        j = getByteLength(arrays[i]) * 2 > 72 ? 2 : 1;
        line = j == 2 && i != 0 ? 10 - recordtop2 : 0;
        appline = j == 1 && line == 0 ? (10 - recordtop2) : 0;
        topvalue2 += line;
        topvalue2 += appline;
        LODOP.ADD_PRINT_TEXT(topvalue2 + "mm", "52mm", "10mm", "5.29mm", arrays2[i]);
        LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
        recordtop2 = j == 2 ? 0 : 5.29;
    }
    var totalNum = 0;
    //第三列
    for (var i = 0; i < arrays3.length; i++) {
        j = getByteLength(arrays[i]) * 2 > 72 ? 2 : 1;
        line = j == 2 && i != 0 ? 10 - recordtop3 : 0;
        appline = j == 1 && line == 0 ? (10 - recordtop3) : 0;
        topvalue3 += line;
        topvalue3 += appline;
        LODOP.ADD_PRINT_TEXT(topvalue3 + "mm", "62mm", "12mm", "5.29mm", arrays3[i]);
        LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
        recordtop3 = j == 2 ? 0 : 5.29;
        totalNum += parseFloat(arrays3[i]);
    }
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "74mm", "3.29mm", "---------------------------------------------------------------------------------");
    topvalue += 3.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "10mm", "64mm", "5.29mm", "小计：" + subTotal);
    topvalue += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "10mm", "64mm", "5.29mm", "优惠：" + discount);
    topvalue += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "10mm", "64mm", "6.29mm", "已付：" + handle);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
    topvalue += 6.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "74mm", "5.29mm", "客人：" + guest);
    topvalue += 6.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "74mm", "5.29mm", "联系电话：" + guestphone);
    topvalue += 6.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "74mm", "5.29mm", "地址：" + address);
    topvalue += 5.29;
    if (getByteLength("地址：" + address) * 2 > 72) lineHeight += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "74mm", "5.29mm", "打印时间：" + printTime);
    topvalue += 5.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "74mm", "6.29mm", "谢谢惠顾!");
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    topvalue += 6.29;
    LODOP.ADD_PRINT_TEXT(topvalue + "mm", "0mm", "74mm", "5.29mm", companyName);
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    LODOP.SET_PRINT_PAGESIZE(3, 740, 50, "");//设置打印的宽高
}

function PrintWaitLoop() {
    if (!waiting) {
        LODOP = getCLodop();
        LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true);
        if (LODOP.CVERSION) {
            LODOP.On_Return_Remain = true;
            LODOP.On_Return = function (TaskID, Value) {
                if (TaskID == TaskID1) {
                    P_ID = Value;
                    if ((P_ID != "")) { waiting = true; c = 0; loop = loop + 1; setTimeout("PrintWaitLoop()", 200); } else { document.getElementById('T14').value = "已停止循环！"; };
                } else
                    if (TaskID == TaskID2) {
                        if (Value == 0) {
                            waiting = false;
                            setValue(false);
                        }
                        setTimeout("PrintWaitLoop()", 200);
                    };
            };
            TaskID1 = LODOP.PRINT();
        } else {
            P_ID = LODOP.PRINT();
            if ((P_ID != "")) { waiting = true; c = 0; loop = loop + 1; setTimeout("PrintWaitLoop()", 200); } else { document.getElementById('T14').value = "已停止循环！"; }
        };
    } else {
        c = c + 1;
        document.getElementById('T12A').value = P_ID;
        document.getElementById('T14').value = "正打印第" + loop + "个任务(JOB代码" + P_ID + ")等待：" + c / 5 + "秒";
        if (LODOP.CVERSION) {
            TaskID2 = LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID);
        } else {
            if (!LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID)) {
                waiting = false;//以是否还在队列为准
                setValue(false);
            }
            setTimeout("PrintWaitLoop()", 200);
        };
    }

}


function PrintWait() {
    if (c > 0) { //alert("正打印中..."); 
        return;
    }
    LODOP = getCLodop();
    LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true);
    if (LODOP.CVERSION) {
        LODOP.On_Return = function (TaskID, Value) {
            P_ID = Value;
            document.getElementById('T12A').value = P_ID;
            if (P_ID != "") { c = 0; C_WaitFor(); };
        };
        TaskID = LODOP.PRINT();
    } else {
        P_ID = LODOP.PRINT();
        document.getElementById('T12A').value = P_ID;
        if (P_ID != "") { c = 0; WaitFor(); };
    };
}
function WaitFor() {
    c = c + 1;
    document.getElementById('T12B').value = "正等待(JOB代码是" + P_ID + ")打印结果：" + c + "秒";
    t = setTimeout("WaitFor()", 1000);
    //t2 = setTimeout('getstatus()', 1000);
    if (LODOP.GET_VALUE("PRINT_STATUS_OK", P_ID)) {
        //clearTimeout(t2);
        clearTimeout(t);
        document.getElementById('T12B').value = "打印成功！";
        setValue(false);
        c = 0;
        //alert("打印成功！");
    } if ((!LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID)) && (c > 0)) {
        //clearTimeout(t2);
        clearTimeout(t);
        document.getElementById('T12B').value = "打印任务被删除！";
        setValue(false);
        c = 0;
        //alert("打印任务被删除！");
    } else if (c > 50) {
        clearTimeout(t);
        document.getElementById('T12B').value = "打印超时(50秒)！";
        c = 0;
        ControlPrinterPURGE(document.getElementById('T12A').value);
        $.get("/order/errorprintresult?oid="+getUrlParam('oid'), function (result) {});
        setValue(false);
        //alert("打印超过30秒没捕获到成功状态！");
    };
};
function C_WaitFor() {
    c = c + 1;
    document.getElementById('T12B').value = "正等待(JOB代码是" + P_ID + ")打印结果：" + c + "秒";
    t = setTimeout("C_WaitFor()", 1000);
    //t2 = setTimeout('getstatus()',1000)
    LODOP.On_Return_Remain = true;
    LODOP.On_Return = function (TaskID, Value) {
        if (TaskID == TaskID1) {
            if (Value == 1) {
                alert(TaskID + " " + Value);
                //clearTimeout(t2);
                clearTimeout(t);
                document.getElementById('T12B').value = "打印成功！";
                setValue(false);
                c = 0;
                //alert("打印成功！");
            };
        } else
            if (TaskID == TaskID2) {
                if (Value == 0) {
                    //clearTimeout(t2);
                    clearTimeout(t);
                    document.getElementById('T12B').value = "打印任务被删除！";
                    setValue(false);
                    c = 0;
                    //alert("打印任务被删除！");
                };
            };
    };
    TaskID1 = LODOP.GET_VALUE("PRINT_STATUS_OK", P_ID);
    TaskID2 = LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID);
    if (c > 50) {
        clearTimeout(t);
        document.getElementById('T12B').value = "打印超时(50秒)！";
        c = 0;
        ControlPrinterPURGE(document.getElementById('T12A').value);
        $.get("/order/errorprintresult?oid="+getUrlParam('oid'), function (result) {});
        setValue(false);
        //alert("打印超过30秒没捕获到成功状态！");
    };
};

function getUrlParam(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return unescape(r[2]); return null;
}

//从JOB代码找出打印机序号：
function GetPrinterIDfromJOBID(strJOBID) {
    var intPos = strJOBID.indexOf("_");
    if (intPos < 0) { return strJOBID; } else { return strJOBID.substr(0, intPos); }
}

//清理打印任务:
function ControlPrinterPURGE(strJOBID) {
    strPrinterID = GetPrinterIDfromJOBID(strJOBID);
    LODOP = getCLodop();
    if (LODOP.CVERSION) {
        LODOP.On_Return = function (TaskID, Value) { //alert("清理结果:" + Value); 
        };
        LODOP.SET_PRINT_MODE("CONTROL_PRINTER:" + strPrinterID, "PURGE");
        return;
    } else {
        var strResult = LODOP.SET_PRINT_MODE("CONTROL_PRINTER:" + strPrinterID, "PURGE");
        //alert("清理结果:" + strResult);
    };
}



function setValue(obj) {
    parent.document.getElementById("txt_result").value = obj;
}

function getStatusValue(ValueType, ValueIndex, oResultOB) {
    LODOP = getCLodop();
    if (LODOP.CVERSION) LODOP.On_Return = function (TaskID, Value) { oResultOB.value = Value; };
    var strResult = LODOP.GET_VALUE(ValueType, ValueIndex);
    if (!LODOP.CVERSION) return strResult; else return "";
}

function getstatus() {
    document.getElementById('T9').value = getStatusValue('PRINT_STATUS_LIST', document.getElementById('T12A').value, document.getElementById('T9'))
}